package com.google.srclib.stock;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;

/**
 * 
 * @author Yuyao Liang
 * 
 */
public class StockCodeCollector extends Collector {

	public final String Version = Messages
			.getString("StockCodeCollector.VERSION"); //$NON-NLS-1$

	private static StockCodeCollector instance = null;
	private final String CollectorURL = Messages
			.getString("StockCodeCollector.URL"); //$NON-NLS-1$
	public static final String[] ExchangeNames = new String[] {
			Messages.getString("StockCodeCollector.NYSE"), //$NON-NLS-1$
			Messages.getString("StockCodeCollector.NASDAQ"), //$NON-NLS-1$ 
			Messages.getString("StockCodeCollector.AMEX"), //$NON-NLS-1$
			Messages.getString("StockCodeCollector.OTCBB"), //$NON-NLS-1$
			Messages.getString("StockCodeCollector.USMF") }; //$NON-NLS-1$ 

	private final String location_href_url = Messages
			.getString("StockCodeCollector.LOCATION_HREF_URL"); //$NON-NLS-1$
	private final String extension = Messages
			.getString("StockCodeCollector.EXTENSION"); //$NON-NLS-1$

	public static StockCodeCollector getInstance() {
		if (instance == null) {
			instance = new StockCodeCollector();
		}
		return instance;
	}

	/**
	 * @param names
	 *            Exchange name array
	 * @return StockSymbolDataSet[]
	 * @throws SQLException
	 */
	public StockSymbolDataSet[] getSymbolList(String[] names)
			throws SQLException {
		StockSymbolDataSet[] ss = new StockSymbolDataSet[names.length];
		for (int i = 0; i < names.length; i++) {
			ss[i] = getSymbolList(names[i]);
		}
		return ss;
	}

	/**
	 * @param name
	 *            Exchange Name
	 * @return StockSymbolDataSet
	 * @throws SQLException
	 */
	private StockSymbolDataSet getSymbolList(String name) throws SQLException {
		StockSymbolDataSet ss = new StockSymbolDataSet();
		for (char a = 'A'; a <= 'Z'; a++) {
			ss = getSymbolinPage(ss, name, a);
		}
		return ss;
	}

	/**
	 * @param ss
	 *            StockSymbolDataSet to append information to the data set
	 * @param exchangename
	 *            Exchange name
	 * @param alphabet
	 * @return
	 */
	private StockSymbolDataSet getSymbolinPage(StockSymbolDataSet ss,
			String exchangename, char alphabet) {
		String url = CollectorURL + exchangename + "/" + alphabet + extension; //$NON-NLS-1$
		String search_url = location_href_url + exchangename;
		String break_url = Messages
				.getString("StockCodeCollector.BREAKDOWN_URL"); //$NON-NLS-1$
		String code, name;
		URL targetUrl;
		try {
			targetUrl = new URL(url);

			URLConnection targetUrlCon = targetUrl.openConnection();
			targetUrlCon.setDoOutput(true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					targetUrlCon.getInputStream()));
			while (true) {
				String line = in.readLine();
				if (line == null)
					break;
				if (line.contains(search_url)) {
					code = line.substring(line.indexOf(search_url)
							+ search_url.length() + 1);
					name = code;
					code = code.substring(0, code.indexOf(extension));
					name = name.substring(name.indexOf(break_url)
							+ break_url.length());
					name = name.substring(0, name.indexOf(Messages
							.getString("StockCodeCollector.END_URL"))); //$NON-NLS-1$
					ss.addRow(code, name);
				}
			}
		} catch (Exception e) {
			System.out.println(Messages
					.getString("StockCodeCollector.MALFORMED_URL_ERROR") //$NON-NLS-1$
					+ e.getMessage());
		}
		System.out.println(exchangename + "    " + ss.getCount()); //$NON-NLS-1$
		return ss;
	}

	@Override
	public String getVersion() {
		return Version;
	}

}
